package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.TaxMenuModel;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2018/5/29.
 */

public class TaxSearchAdapter extends RecyclerView.Adapter<TaxSearchAdapter.ViewHolder> {

    private Context context;

    private LayoutInflater inflater;

    private List<TaxMenuModel.Data> list;

    public TaxSearchAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void addData(List<TaxMenuModel.Data> list) {
        if (this.list != null) {
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_tax_search_menu, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final TaxMenuModel.Data data = list.get(position);
        if (data != null) {
            ImageUtil.loadImage(context, data.getIcon(), holder.img);
            holder.name.setText(data.getName());
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goTaxSearchDetailActivity(context, data.getName(), data.getImg());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView name;

        private ImageView img;

        private View view;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            img = itemView.findViewById(R.id.img);
            view = itemView;
        }
    }

    public void clear() {
        if (list != null) {
            list.clear();
            list = null;
        }
    }
}
